
<template>
  <el-dialog v-model="dialogFormVisible" :title="title" width="1200">
    <el-form :model="form" ref="formRef" label-width="120" style="max-width: 1200px" :rules="rules">
      <div class="form-scroll">
        <el-row :gutter="20">

          <el-col :span="12">
            <el-form-item label="副本名称" prop="copyName">
              <el-select v-model="form.copyName" placeholder="请选择副本名称" clearable filterable @change="copyNameChange" >
                <el-option  v-for="(item, index) in copyList"
                            :key="item.copyName"
                            :value="item.copyName"
                            :label="item.copyName"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="副本工资" prop="copyWages">
              <el-input v-model="form.copyWages" placeholder="请填写副本工资" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="副本消费" prop="copyUse">
              <el-input v-model="form.copyUse" placeholder="请填写副本消费" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="copyDesc">
              <el-input v-model="form.copyDesc" placeholder="请填写备注" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="玄晶数量" prop="copyXuanJing">
              <el-input v-model="form.copyXuanJing" placeholder="请填写玄晶数量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="特殊数量" prop="copySpecial">
              <el-input v-model="form.copySpecial" placeholder="请填写特殊数量" />
            </el-form-item>
          </el-col>
        </el-row>
      </div>
    </el-form>
    <div>
      <el-button type="primary" @click="onSubmit">{{ isEdit? '编辑': '新增' }}</el-button>
      <el-button @click="dialogFormVisible = false">取消</el-button>
    </div>
  </el-dialog>
</template>

<script setup>
    // 方法
    import { ref, defineExpose, defineEmits } from 'vue'
    const instance = getCurrentInstance();
    const { proxy } = instance || {};
    const { face_type } = proxy.useDict("face_type")
    import methods from '@/utils/methods.js';

    // 接口
    import { addCopyRecord, getCopyRecord, updateCopyRecord } from '@/api/jx3/copyRecord.js'
    import { listCopy } from '@/api/jx3/copy.js'

    // 组件
    import imageUpload from '@/components/ImageUpload/index.vue'

    // 显示表单
    const dialogFormVisible = ref(false)
    // 弹窗标题
    const title = ref('新增副本记录')
    // 是否为编辑
    const isEdit = ref(false)
    // 表单节点
    const formRef = ref(null)
    // 表单
    const form = ref({
      id: undefined, // ID
      userId: undefined, // 用户id
      copyName: undefined, // 副本名称
      copyWages: 0, // 副本工资
      copyUse: 0, // 副本消费
      copyDesc: undefined, // 备注
      copyXuanJing: 0, // 玄晶数量
      copySpecial: 0, // 特殊数量
      copyUrl: undefined, // 图片
      createTime: undefined, // 创建时间
      createBy: undefined, // 创建人
      updateTime: undefined, // 更新时间
    })
    // 表单
    const formYs = ref({
      id: undefined, // ID
      userId: undefined, // 用户id
      copyName: undefined, // 副本名称
      copyWages: 0, // 副本工资
      copyUse: 0, // 副本消费
      copyDesc: undefined, // 备注
      copyXuanJing: 0, // 玄晶数量
      copySpecial: 0, // 特殊数量
      copyUrl: undefined, // 图片
      createTime: undefined, // 创建时间
      createBy: undefined, // 创建人
      updateTime: undefined, // 更新时间
    })
    // 验证
    const rules = ref({
      // 用户id
      userId: [
        { required: true, message: '请填写用户id', trigger: 'blur' }
      ],
      // 副本名称
      copyName: [
        { required: true, message: '请填写副本名称', trigger: 'blur' }
      ],
      // 玄晶数量
      copyXuanJing: [
        { required: true, message: '请填写玄晶数量', trigger: 'blur' }
      ],
      // 特殊数量
      copySpecial: [
        { required: true, message: '请填写特殊数量', trigger: 'blur' }
      ],
      // 创建时间
      createTime: [
        { required: true, message: '请填写创建时间', trigger: 'blur' }
      ],
      // 创建人
      createBy: [
        { required: true, message: '请填写创建人', trigger: 'blur' }
      ],
      // 更新时间
      updateTime: [
        { required: true, message: '请填写更新时间', trigger: 'blur' }
      ],
    })

    const emits = defineEmits(['change'])


    // 获取数据
    const copyList = ref([])
    const getCopyList = () => {
      listCopy({
        pageSize: 100,
        pageNum: 1,
      }).then(res => {
        copyList.value = res.rows
      })
    }
    getCopyList()

    // 获取副本信息
    const copyNameChange = (e) => {
      for (let i of copyList.value) {
        if(i.copyName === e) {
          form.value.copyUrl = i.copyDesc
          break;
        }
      }
    }

    // 打开方法
    const open = () => {
      for (let i in form.value) {
        form.value[i] = formYs.value[i]
      }
      dialogFormVisible.value = true
      isEdit.value = false
      title.value = '新增副本记录'
    }
    // 编辑
    const edit = (data) => {
      getCopyRecord(data.id).then(res => {
        for (let i in res.data) {
          form.value[i] = data[i]
        }
      })
      isEdit.value = true
      dialogFormVisible.value = true
      title.value = '编辑副本记录'
    }
    // 关闭方法
    const close = () => {
      dialogFormVisible.value = false
    }

    // 提交
    const onSubmit = async () => {
      await formRef.value.validate((valid, fields) => {
        if (valid) {
          const params = methods.deepClone(form.value)
          if(params.id) {
            updateCopyRecord(params).then(res => {
              if(res.code === 200) {
                proxy.$modal.msgSuccess(title.value + '成功');
                close()
                emits('change')
              }
            })
          }else {
            addCopyRecord(params).then(res => {
              if(res.code === 200) {
                proxy.$modal.msgSuccess(title.value + '成功');
                close()
                emits('change')
              }
            })
          }
        }
      })
    }

    defineExpose({
      open, close, edit
    })
</script>

<style scoped lang="scss">

</style>
